package com.kgo.log.logtools.config;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.kgo.log.logtools.cache.ListenFileList;
import com.kgo.log.logtools.file.watch.FileListen;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Component
public class SystemConfig {
    private Logger log = LoggerFactory.getLogger(SystemConfig.class);
    private final String userZone = "users";
    private final String monitorPathZone = "monitorPath";
    @Autowired
    private FileListen fileListen;
    public static List<String> watchFile;
    public static Map<String,List<String>> userFile;

    public void init(JSONObject configJson){
        watchFile = configJson.getJSONArray(monitorPathZone).toJavaList(String.class);
        log.debug("初始化文件目录监听列表 ：{} ",watchFile);
        initSystemFileListen();
    }
    private void initSystemFileListen(){
        if(ObjectUtil.isEmpty(watchFile)) return;
        log.debug("监听文件 ：");
        for(String path : watchFile){
            log.debug("监听路径 ：{} ",path);
            fileListen.startListen(path);
            ListenFileList.initFiles(path);
        }
        log.debug("监听文件 结束");
    }
}
